package com.xiaoshuidi.cloud.module.iot.uitils;

import org.springframework.util.StringUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by Arvin on 5/11/18.
 */
public class DateUtils {
    public static final String DATETIME_ISO_NOT_CONTAINS_ZONE = "yyyy-MM-dd HH:mm:ss";
    public static final String DATE_ISO_NOT_CONTAINS_ZONE = "yyyy-MM-dd";
    public static final String[] zodiacArr = {"猴", "鸡", "狗", "猪", "鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊"};
    public static final int[] arr = new int[]{20, 19, 21, 21, 21, 22, 23, 23, 23, 23, 22, 22};
    public static final String s = "魔羯水瓶双鱼白羊金牛双子巨蟹狮子处女天秤天蝎射手魔羯";

    /**
     * 根据日期获取生肖
     *
     * @return
     */
    public static String getZodica(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return zodiacArr[cal.get(Calendar.YEAR) % 12];
    }

    /**
     * 根据日期获取星座
     *
     * @return
     */
    public static String getConstellation(int month, int day) {
        try {
            int index = month * 2 - (day < arr[month - 1] ? 2 : 0);
            return s.substring(index, index + 2) + "座";
        } catch (Exception e) {
            return "";
        }
    }

    public static String getTimeStr(long timeMilles) {
        if (timeMilles <= 0) {
            timeMilles = System.currentTimeMillis();
        }

        SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss");
        return sf.format(new Date(timeMilles));
    }

    public static String getCurrentTimeStr() {
        SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");
        return sf.format(new Date());
    }

    //获取指定日期的下一天
    public static String getNextFormatDate(Date date, String pattern) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, 1);
        Date newDate = calendar.getTime();
        String stringDate = DateformatUtils.formatDate(newDate, pattern);
        return stringDate;
    }

    /**
     * 判断时间段是否在时间段内
     *
     * @param nowTime
     * @param beginTime
     * @param endTime
     * @return
     */
    public static boolean belongCalendar(Date nowTime, Date beginTime, Date endTime) {
        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);
        Calendar begin = Calendar.getInstance();
        begin.setTime(beginTime);
        Calendar end = Calendar.getInstance();
        end.setTime(endTime);

        if (end.getTimeInMillis() < begin.getTimeInMillis()) {
            end.add(Calendar.DATE, 1);
        }
        if (date.getTimeInMillis() < begin.getTimeInMillis() && date.getTimeInMillis() < end.getTimeInMillis()) {
            date.add(Calendar.DATE, 1);
        }
        if (date.after(begin) && date.before(end)) {
            return true;
        } else {
            return false;
        }
    }


    public static boolean compareDate(Date time1, Date time2, Integer day) {
        long time = 1000 * 3600 * 24;//一天的时间(秒)
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date parse1 = null;
        Date parse2 = null;
        try {
            parse1 = dateFormat.parse(dateFormat.format(time1));
            parse2 = dateFormat.parse(dateFormat.format(time2));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long l = parse1.getTime() - parse2.getTime();
        if (day != null) {
            return (0 < l && l <= day * time);
        } else {
            return l <= 0;
        }
    }

    public static String getTimeStrBySdf(long timeMilles, DateFormat format) {
        if (timeMilles <= 0) {
            timeMilles = System.currentTimeMillis();
        }
        return format.format(new Date(timeMilles));
    }

    /**
     * 将日期字符串 按照指定格式转为 日期对象
     *
     * @param dateToken      日期字符串,当未传递时，默认返沪当前日期
     * @param dateTimeFormat 默认 yyyy-MM-dd HH:mm:ss
     * @return 日期对象, 当解析异常时，返回null
     */
    public static Date parseDateInCST(String dateToken, String dateTimeFormat) {
        if (StringUtils.isEmpty(dateTimeFormat)) {
            dateTimeFormat = DATETIME_ISO_NOT_CONTAINS_ZONE;
        }
        if (StringUtils.isEmpty(dateToken)) {
            return new Date();
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateTimeFormat);
        try {
            return simpleDateFormat.parse(dateToken);
        } catch (ParseException e) {
            return null;
        }
    }

    public static Date parseDateInCST(String dateToken) {
        return parseDateInCST(dateToken, DATETIME_ISO_NOT_CONTAINS_ZONE);
    }
}
